<?php
/**
 * Helium
 *
 * @category   Upload
 * @package    System_Upload
 * @subpackage PHP
 * @copyright  Baotq and Daniel Chivas
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Upload.php 2012-04-08
 */
class System_Upload extends Zend_Db_Table{ 
	
    /**
     * @var array
     */
    protected $_scope = null;
    
    /**
     * @var array
     */
    protected $_config = null;
    
    /**
     * @var string
     */
    protected $_path = null;
    
    /**
     * @var integer
     */
     protected $_filename = null;
    
    /**
     * @var integer
     */
    protected $_prefix = null; 
    /**
     * @var varchar
     */
    protected $_size = null;
    
    /**
     * @var integer
     */
    protected $_extensionallow = null;
    
    /**
     * __construct()
     *
     * @param array $options
     */
    public function __construct($config = array()) {
        $this->_config = config::get('upload');
        parent::__construct($config);
    }
    
    /**
     * setScope()
     *
     * @param string $scope
     */
    public function setScope($scope)
    {
        $array = array("public","private" ,"");
        if(in_array($scope,$array))
        {
            $this->_scope = $scope;
        }else{
            require_once 'Zend/Locale/Exception.php';
            throw new Zend_Locale_Exception("<font color='red' >Thiết lập phạm vi lưu trữ không hợp lệ ( scope = $scope ), chỉ được chỉ định trong phạm vi public hoặc private </font>");
        }
        return;
    }

    /**
     * getScope()
     *
     * @return string
     */
    public function getScope()
    {        
        if($this->_scope == null){
            return "public";
        }else{
            return $this->_scope;
        }
    }
    /**
     * setPath()
     *
     * @param string $path
     */
    public function setPath($path)
    {
        $this->_path = $path;
        return;
    }

    /**
     * getPath()
     *
     * @return string
     */
    public function getPath()
    {
        if($this->_path == null){
            return $this->_config['public']['path'];
        }else{
            return $this->_path;
        }
    }
    
    /**
     * setId()
     *
     * @param integer $name
     */
    public function setFileName($name)
    {
        $this->_filename = $name;
        return;
    }

    /**
     * getId()
     *
     * @return integer
     */
    public function getFileName()
    {
      return $this->_filename;
    }
    
    /*
     * setPrefix()
     * 
     * @param string $prefix
     */
    public function setPrefix($prefix){
    	if($prefix){
    		$this->_prefix = $prefix;
    	}
    	return;
    }
    
    public function getPrefix(){
    	if($this->_prefix != null){
    		$prefix = $this->_prefix;
    	}
    	else{
    		$prefix = '';
    	}
    	return $prefix;
    }
    
    /**
     * setSize()
     *
     * @param integer $size
     */
    public function setSize($size)
    {
        if(is_int($size))
        {
            $this->_size = $size;
        }else{
            require_once 'Zend/Locale/Exception.php';
            throw new Zend_Locale_Exception("<font color='red' >Thiết lập giới hạn dung lượng tập tin không hợp lệ ( size = $size ), size phải là một giá trị Integer  </font>");
        }
        return;
    }

    /**
     * getSize()
     *
     * @return integer
     */
    public function getSize()
    {
        if($this->_size == null){
            return $this->_config['size']['max'];
        }else{
            return $this->_size;
        }
    }
    
    /**
     * setSize()
     *
     * @param string $ext
     */
    public function setExtensionallow($ext)
    {
        $this->_extensionallow = $ext;
        return;
    }

    /**
     * getExtensionallow()
     *
     * @return string
     */
    public function getExtensionallow()
    {
        if($this->_extensionallow == null){
            $uploadConfig = config::get('upload');
            $this->_extensionallow = $uploadConfig['allow'];
        }
        return $this->_extensionallow;
    }
    
    /**
     * upload()
     *
     * @return array
     */
    public function upload(){
        if($this->getScope() == 'public'){
            $data = $this->uploadPublicMultiFile();
            return $data;
        }
    }
    private function uploadPublicMultiFile(){
        
        $allowList  = $this->getExtensionallow();
        $allow      = explode(',',$allowList);
        //$stt        = 0;
        $key        = '';
        $data       = array();
        foreach(array_keys($_FILES) as $sub)
		{
            $key = $sub;
        }
        $cnt = count($_FILES[$key]['name']);
        $name = $this->getFileName();
        $prefix = $this->getPrefix();
        
        
        if(is_array($_FILES[$key]['name'])){
	        for($i = 0; $i < $cnt; $i++){
	            if ($_FILES[$key]['name'][$i] != "") {
	                $ext = pathinfo($_FILES[$key]['name'][$i]);    
	                $stt++;
	                if($name == ''){
	                	$_FILES[$key]['name'][$i] = md5(time()).'.'.$ext['extension'];
	                }        
	                else{
	                	$_FILES[$key]['name'][$i] = $name.'_'.$stt.'.'.$ext['extension'];
	                }
	            }
	        }
        }else{
        	if ($_FILES[$key]['name'] != "") {
               $ext = pathinfo($_FILES[$key]['name']);
               $stt++;
        		if($name == ''){
                	$_FILES[$key]['name'] = md5(time()).'.'.$ext['extension'];
                }
                else{
                	$_FILES[$key]['name'] = $name.'.'.$ext['extension'];
                }
            }
        }
       
        $uploader = new Zend_File_Transfer_Adapter_Http();
        $info = $uploader->getFileInfo();
        $ii = 0;            
        foreach(array_keys($info) as $sub){
            
            $file_info      = $info[$sub];
            $re_filename    = $file_info['name'];
            $fileName       = explode('.',$info[$sub]['name']);
            $ext            = $fileName[count($fileName)-1];

            if($ext != "" && $re_filename)
            {
            	$is_valid = false;
                if($file_info['size'] < $this->getSize()){
                	$is_valid = true;
                    $status = "saved";                    
                }
                if (!in_array($ext, $allow)){
                	$is_valid = false;
                    $status = "invalid";
                }
                if($file_info['size'] > $this->getSize()){
                	$is_valid = false;
                    $status = "oversize";
                }                
                // upload dữ liệu
                
                if($is_valid == true){
                	$uploader->setDestination($this->getPath());
                	$uploader->receive($re_filename);
                }
                $data[$ii]['id']        = $id;
                $data[$ii]['name']      = $re_filename;
                $data[$ii]['mime']      = $ext;
                $data[$ii]['path']      = $this->getPath();
                $data[$ii]['status']    = $status;
                $ii++;
            }
        }
        return $data;
	}
}